package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.grandchild.GrandChildProfession;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GrandChildProfessionDaoImpl extends TempDataObjectDao<GrandChildProfession> {

    public Map<Integer, GrandChildProfession> getGrandChildProfessionMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_grand_child_profession where userId=?";
        Map<Integer, GrandChildProfession> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                GrandChildProfession data = parse(rs);
                dataMap.put(data.getProfessionId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(GrandChildProfession data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_grand_child_profession(userId,professionId,grandChildCount,skillLv) values (?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getProfessionId());
            call.setLong(i++, data.getGrandChildCount());
            call.setInt(i++, data.getSkillLv());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(GrandChildProfession data) {
        PreparedStatement call = null;
        String sql = "update t_u_grand_child_profession set grandChildCount=?,skillLv=? where userId=? and professionId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getGrandChildCount());
            call.setInt(i++, data.getSkillLv());
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getProfessionId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected GrandChildProfession parseData(ResultSet rs) throws SQLException {
        GrandChildProfession data = new GrandChildProfession();
        data.setUserId(rs.getLong("userId"));
        data.setProfessionId(rs.getInt("professionId"));
        data.setGrandChildCount(rs.getLong("grandChildCount"));
        data.setSkillLv(rs.getInt("skillLv"));
        return data;
    }
}
